package com.admin.controller;

import com.admin.service.RoleService;
import com.admin.util.CommonUtil;
import com.admin.util.LogUtil;
import com.admin.util.Result;
import com.data.entity.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
@Api(tags = "角色管理")
/**
 * 角色管理
 */
public class RoleController {
    @Autowired
    private RoleService roleService;
    private final Logger logger = LogUtil.getBussinessLogger();

    /**
     * 角色列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("角色：列表")
    public Result list() {
        Result result = new Result();
        try {
            result.setCode(Result.Code.SUCCESS);
            result.setData(roleService.list());
            result.setCount(roleService.count());
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 角色菜单关联信息
     * @param roleId 角色id
     * @return
     */
    @GetMapping("/relMenu")
    @ApiOperation("角色：关联菜单信息")
    public Result getRel(@RequestParam(value = "roleId", required = true) Long roleId) {
        Result result = new Result();
        try {
            result.setCode(Result.Code.SUCCESS);
            result.setData(roleService.relMenu(roleId));
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 添加或修改
     * @param role
     * @return
     */
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(Role role) {
        Result result = new Result();
        try {
            List<Role> roles = roleService.list();
            if (!CommonUtil.isEmpty(role.getId())) {
                //修改信息
                Role role1 = roleService.getInfo(role.getId());
                role1 = role;
                role1.setUpdatetime(new Date());  //更新
                roleService.edit(role1);
                result.setMessage(Result.Code.SUCCESS, "操作成功！");
            } else {
                for (Role role2:roles) {
                    if (role2.getName().equals(role.getName())){    //角色名称对比
                        result.setMessage(Result.Code.SYSTEMERROR, "已存在该角色,不能重复添加");
                        return result;
                    }
                }
                roleService.add(role);
                result.setMessage(Result.Code.SUCCESS, "添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }


    /**
     * 删除信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Long id){
        Result result=new Result();
        try {
            roleService.delete(id);
            result.setCode(Result.Code.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            result.setCode(Result.Code.SYSTEMERROR);
        }
        return result;
    }

    /**
     * 获取角色信息
     * @param id    角色id
     * @return
     */
    @GetMapping("/getInfo")
    public Result getInfo(Long id) {
        Result result = new Result();
        try {
            Role role = roleService.getInfo(id);
            result.setCode(Result.Code.SUCCESS);
            result.setData(role);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }

        return result;
    }



    /**
     * 保存菜单与角色关联信息
     * @param menuIds   菜单id数组
     * @param roleId    角色id
     * @return
     */
    @GetMapping("/addRelMenu")
    @ApiOperation("角色：保存菜单与角色关联信息")
    public Result addRelMenu(String[] menuIds, Long roleId) {
        Result result = new Result();
        try {
            result.setCode(Result.Code.SUCCESS);
            List<Long> menus = new LinkedList<>();
            for (int i = 0; i < menuIds.length; i++) {
                menus.add(Long.parseLong(menuIds[i]));
            }
            roleService.addRoleMenu(roleId, menus);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }
}
